<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="中文名称" prop="companyNameCn">
        <el-input v-model="queryParams.companyNameCn" placeholder="请输入中文名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="英文名称" prop="companyNameEn">
        <el-input v-model="queryParams.companyNameEn" placeholder="请输入英文名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="企业中心" prop="url">
        <el-input v-model="queryParams.url" placeholder="请输入企业中心" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="图片地址" prop="pictureUrl">
        <el-input
          v-model="queryParams.pictureUrl"
          placeholder="请输入图片地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="图片名" prop="prctureName">
        <el-input
          v-model="queryParams.prctureName"
          placeholder="请输入图片名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:picture:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:picture:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:picture:remove']">删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:picture:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="pictureList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="${comment}" align="center" prop="id" /> -->
      <el-table-column label="中文名称" align="center" prop="companyNameCn" />
      <el-table-column label="英文名称" align="center" prop="companyNameEn" />
      <el-table-column label="企业id" align="center" prop="url" />
      <el-table-column label="图片地址" align="center" prop="pictureUrl">
        <template slot-scope="scope">
          <image-preview :src="scope.row.pictureUrl" style="width: 100px;" />
        </template>
      </el-table-column>
      <el-table-column label="行数" align="center" prop="row" />
      <!-- <el-table-column label="图片名" align="center" prop="prctureName" /> -->
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <!-- <el-table-column label="删除标志" align="center" prop="delFlag" />
      <el-table-column label="创建者" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
        <el-table-column label="更新者" align="center" prop="updateBy" /> -->
      <!-- <el-table-column label="更新时间" align="center" prop="updateTime" >
        <template slot-scope="scope">
          {{ parseTime(scope.row.updateTime) }}
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:picture:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:picture:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改合作伙伴对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="中文名称" prop="companyNameCn">
          <el-input v-model="form.companyNameCn" placeholder="请输入中文名称" />
        </el-form-item>
        <el-form-item label="英文名称" prop="companyNameEn">
          <el-input v-model="form.companyNameEn" placeholder="请输入英文名称" />
        </el-form-item>
        <el-form-item label="企业中心" prop="url">
          <!-- <el-input v-model="form.url" placeholder="请输入企业中心" /> -->
          <el-select v-model="form.url" filterable clearable placeholder="请选择企业" style="width: 100%;">
            <el-option v-for="item in listEnterprise" :key="item.id" :label="item.enterpriseName"
              :value="String(item.id)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="行数" prop="row">
          <el-select v-model="form.row" placeholder="请选择行数" style="width: 100%;">
            <el-option label="1" value="1"></el-option>
            <el-option label="2" value="2"></el-option>
            <el-option label="3" value="3"></el-option>
            <el-option label="4" value="4"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="logo" prop="pictureUrl">
          <image-upload v-model="form.pictureUrl" url="/file/home/upload" :fileType="['png', 'jpg', 'jpeg', 'gif']"
            :fileSize="5" :limit="1" />
        </el-form-item>

        <!-- <el-form-item label="图片地址" prop="pictureUrl">
          <el-input v-model="form.pictureUrl" placeholder="请输入图片地址" />
        </el-form-item>
        <el-form-item label="图片名" prop="prctureName">
          <el-input v-model="form.prctureName" placeholder="请输入图片名" />
        </el-form-item> -->
        <!-- <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPicture, getPicture, delPicture, addPicture, updatePicture } from "@/api/system/cooperative";
import { enterpriseListAllList } from "@/api/system/enterprise"
export default {
  name: "Picture",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 合作伙伴表格数据
      pictureList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyNameCn: null,
        companyNameEn: null,
        url: null,
        pictureUrl: null,
        prctureName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {

      },
      listEnterprise: []
    };
  },
  created() {
    this.getList();
    this.listEnterpriseListAll()
  },
  methods: {
    /** 查询合作伙伴列表 */
    getList() {
      this.loading = true;
      listPicture(this.queryParams).then(response => {
        this.pictureList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    listEnterpriseListAll() {
      if (this.listEnterprise && this.listEnterprise.length > 0) {
        return
      }
      enterpriseListAllList().then(response => {
        this.listEnterprise = response.data
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        companyNameCn: null,
        companyNameEn: null,
        url: null,
        pictureUrl: null,
        prctureName: null,
        remark: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加合作伙伴";
      this.listEnterpriseListAll()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      this.listEnterpriseListAll()
      getPicture(id).then(response => {
        this.form = response.data;
        // this.$set(this.form, 'url', Number(response.data.url))
        // console.log(this.form.url==NaN)
        // if (this.form.url == NaN) {
        //   console.log('NaN')
        //   this.$set(this.form, 'url', null)
        // }
        this.open = true;
        this.title = "修改合作伙伴";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePicture(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPicture(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除合作伙伴编号为"' + ids + '"的数据项？').then(function () {
        return delPicture(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/picture/export', {
        ...this.queryParams
      }, `picture_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
